package com.ldk.base.framework.util;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by Administrator on 2016/10/11.
 */
public class DateUtil {
    public static String dbyyyymmddhh24mmss = "yyyy-mm-dd hh24:mi:ss";
    public static String dbyyyymmddhh24mm = "yyyy-mm-dd hh24:mi";
    public static String dbyyyymmddhh24 = "yyyy-mm-dd hh24";
    public static String dbyyyymmdd = "yyyy-mm-dd";
    public static String dbyyyymmdd1 = "yyyyMMdd";
    public static String dbyyyymm = "yyyy-mm";

    public static String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static String yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
    public static String yyyyMMddHH = "yyyy-MM-dd HH";
    public static String yyyyMMdd = "yyyy-MM-dd";
    public static String yyyyMM = "yyyy-MM";
    public static String yyyyMMddHH1 = "yyyyMMddHH";
    public static String yyyyMMddHHmm1 = "yyyyMMddHHmm";
    public static String yyyyMMddHHmmFlood = "yyyy/MM/dd aHH:00:00";

    public static String userid = "";//临时保存登录人id，用于监控对象统计列表数据根据用户所在行政区过滤

    public static Date AddYear(Date date, int year) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(GregorianCalendar.YEAR, year);
        return gc.getTime();
    }

    public static Date AddMonth(Date date, int month) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(GregorianCalendar.MONTH, month);
        return gc.getTime();
    }

    public static Date AddDay(Date date, int day) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(GregorianCalendar.DAY_OF_MONTH, day);
        return gc.getTime();
    }

    public static Date AddHours(Date date, int hours) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(GregorianCalendar.HOUR_OF_DAY, hours);
        return gc.getTime();
    }

    public static int getHours(Date date) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        return gc.get(GregorianCalendar.HOUR_OF_DAY);
    }

    public static Date AddMinute(Date date, int minute) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(GregorianCalendar.MINUTE, minute);
        return gc.getTime();
    }

    public static Date AddSecond(Date date, int second) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(GregorianCalendar.SECOND, second);
        return gc.getTime();
    }

    public static String DatetoString(Date date, String fmt) {
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        return sdf.format(date);
    }


    public static Date StringtoDate(String date, String fmt) {
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        Date currdate = null;
        try {
            currdate = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return currdate;
    }



    public static String longToStr(long time, String fmt) {
        SimpleDateFormat format = new SimpleDateFormat(fmt);
        return format.format(new Date(time));
    }

    public static String OutRunTime(long value) {
        int seconds = (int) value / 1000;
        int minutes = seconds / 60;
        int hour = seconds / 3600;
        long millisecond = value % 1000;
        seconds = seconds % 60;
        minutes = minutes % 60;
        value = value % 1000;
        String result = "";
        if (hour > 0) {
            result += hour + "时";
        }
        if (minutes > 0) {
            result += minutes + "分";
        }
        if (seconds > 0) {
            result += seconds + "秒";
        }
        if (millisecond > 0) {
            result += millisecond + "毫秒";
        }

        return result;
    }

    public static Date LocalDateTimeToUTC(long seconds) {
        Date date = new Date(seconds);
        SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmss);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        return StringtoDate(sdf.format(date), yyyyMMddHHmmss);
    }

    /**
     * Date  转UTC时间
     *
     * @param start 日期
     * @return
     */
    public static Date getDateToUTCTime(Date start) {
        return AddHours(start, -8);
    }


    public static Date UTCToLocalDateTime(long seconds) {
        return new Date(seconds + 1000 * 60 * 60 * 8L);
    }

    /**
     * 判断时间是否和当前时间是同一天
     *
     * @param startTime 时间
     * @return true 表示取实时预警表 false表示取历史预警表
     */
    public static Boolean realDataFlag(Date startTime) {
        if (startTime == null) {
            return true;
        }
        String time = DateUtil.DatetoString(startTime, DateUtil.yyyyMMdd);
        String current = DateUtil.DatetoString(new Date(), DateUtil.yyyyMMdd);
        if (time.equals(current)) {
            return true;
        }
        return false;
    }


    /**
     * 获取两个日期之间的所有日期
     *
     * @param start 开始日期
     * @param end   结束日期
     * @return
     */
    public static List<String> getBetWeenDays(Date start, Date end, String fmt) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat(fmt);
        try {
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            //tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (!tempStart.after(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }



    /**
     * 获取两个日期之间的相差多少小时
     *
     * @param start 开始日期
     * @param end   结束日期
     * @return
     */
    public static long getBetWeenHours(Date start, Date end) {
        long startlong = start.getTime();
        long endlong = end.getTime();

        long seconds = (long) (endlong - startlong) / 1000;
        long hour = seconds / 3600;
        return hour;
    }

    public static Integer getDayNumOfMonth(String ym) {
        if (StringUtils.isBlank(ym)) {
            return null;
        }

        String month = ym.substring(5, 7);
        Integer year = Integer.valueOf(ym.substring(0, 4));
        int days;
        switch (month) {
            case "02": {
                boolean bfalg = year % (year % 100 == 0 ? 4 : 400) == 0 ? false : true;
                if (bfalg) {
                    days = 29;
                } else {
                    days = 28;
                }
                break;
            }
            case "04":
            case "06":
            case "09":
            case "11":
                days = 30;
                break;
            default:     //1,3,5,7,8,10,12
                days = 31;
                break;
        }
        return days;
    }


    public static String getBirthFromIdCard(String idCard) {
        if (idCard.length() != 18 && idCard.length() != 15) {
            return "";
        }
        if (idCard.length() == 18) {
            String year = idCard.substring(6).substring(0, 4);// 得到年份
            String month = idCard.substring(10).substring(0, 2);// 得到月份
            String day = idCard.substring(12).substring(0, 2);// 得到日
            return (year  + month  + day);
        } else if (idCard.length() == 15) {
            String year = "19" + idCard.substring(6, 8);// 年份
            String month = idCard.substring(8, 10);// 月份
            String day = idCard.substring(10, 12);// 得到日
            return (year + month + day);
        }
        return "";
    }
}
